#include "OcrVsTruth.hpp"
#include <QDebug>
#include <QTextStream>
#include "math.h"
#include "LevenshteinDistance.hpp"
//#include "HistogramPlotter.hpp"



float OcrVsTruth::doAction(const QFileInfo &f)
{

    QDir dir = f.absoluteDir();
    QFileInfo truthFileInfo;
    foreach(QFileInfo f1, dir.entryInfoList(QStringList("text*"),QDir::Files,QDir::Name))
    {
        qDebug() << f1.baseName();
        truthFileInfo = f1;
    }

    QFile file1(truthFileInfo.absoluteFilePath());
    if (!file1.open(QIODevice::ReadOnly | QIODevice::Text))
        return -1;



    QTextStream in1(&file1);
    _truth = in1.readAll();


    QString ocrFilePath = f.absoluteDir().absolutePath().append("/") ;
    ocrFilePath.append(f.baseName().replace("image","ocr").append(".txt"));

    qDebug() << ocrFilePath;

    QFile file(ocrFilePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return- 1;

    QTextStream in(&file);
    QString ocr = in.readAll();


    LevenshteinDistance d;
    float nombre = d.distance(_truth.toStdString(),ocr.toStdString());

    return nombre;


}

